package com.ssh;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.Test;

public class FindLongestWord_524 {
	@Test
	public void test() {
		System.out.println(findLongestWord("abpcplea", new ArrayList<String>(Arrays.asList("a","b","c"))));
	
		System.out.println("".compareTo("apple"));
	
	}
	
	public String findLongestWord(String s, List<String> d) {
        int j = -1,n = s.length();
        int[] y = new int[d.size()];
        String z = "";
        int max = 0;
        for (int i = 0; i < n; i++) {
            char c = s.charAt(i);
            //System.out.println("c="+c);
            for (String a : d) {
            	j++;
                if (a.length() <= y[j]) {
                    continue;
                }
                //System.out.println("a.charAt(y[j])="+a.charAt(y[j]));
                if (a.charAt(y[j]) == c) {
                	y[j] = 1+y[j];
                	//System.out.println(i+"--"+j+"--"+y[j]);
                }
                
            }
            j = -1;
        }
        for (int k = 0; k < d.size(); k++) {
        	//System.out.println(y[k]);
            if (y[k] == d.get(k).length() && max <= y[k]) {
            	System.out.println("z="+z);
            	System.out.println(z.compareTo(d.get(k)));
                if (max == y[k]) {
                	if (z.compareTo(d.get(k)) > 0) {
                		z = d.get(k);
                	}
                }else {
                	max = y[k];
                    z = d.get(k);
                }
            }
        }
        return z;
    }
}
